package com.personnalManagerSystem.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.personnalManagerSystem.model.User;
import com.personnalManagerSystem.model.Wage;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-03-27
 */
@Controller
@RequestMapping("/wage")
public class WageController  extends BaseController{
    //查询列表
    @RequestMapping("/list")
    public String list(Model model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){
        baseList(model,option,searchContent,pageNum,pageSize,iWageService);
        return "view/wage";
    }
    //查询上一个月的工资,具体的日期应该控制在五号之前
    @RequestMapping("/prev")
    public String prev(Model  model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){

        //设置默认的页码
        if (pageNum==null || pageNum==0){
            pageNum=1;
        }
        //设置默认的一页显示数
        if (pageSize==null || pageSize==0){
            pageSize=5;
        }
        //创建一个构造器
        QueryWrapper<Wage> wrapper = new QueryWrapper<>();
        if(option!=null
                && searchContent!=null
                && !"".equals(option)
                &&!"".equals(searchContent)){
            wrapper.like(option,searchContent);

        }
        //设置分页
        Page<List<Wage>> pageInfo = PageHelper.startPage(pageNum, pageSize);//返回值是Page类型的数据
        LocalDate now = LocalDate.now();
        int monthValue = now.getMonthValue();
        int dayOfMonth = now.getDayOfMonth();
        //wrapper.eq("state","已发放");
        wrapper.eq("wage_month",monthValue-1);
        wrapper.ge("wage_day",1);
        wrapper.le("wage_day",5);
        //在登录的时候将用户记录存储在了session中
        User user =(User) SecurityUtils.getSubject().getSession().getAttribute("user");
        if("普通用户".equals(user.getDescription())){
            //如果其角色是普通用户,那么只可以查询关于自己的一系列消息,不能出现跨用户访问,否则会出现资源泄露
            wrapper.eq("user_id",user.getUserId());
        }
        iWageService.list(wrapper);
        //设置页面信息
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("option",option);
        model.addAttribute("searchContent",searchContent);

        //return "redirect:/wage/list";
        return "view/wage";
    }

    //查询本月工资
    @RequestMapping("/now")
    public String now(Model  model,
                      String option,
                      String searchContent,
                      Integer pageNum,
                      Integer pageSize){

        //设置默认的页码
        if (pageNum==null || pageNum==0){
            pageNum=1;
        }
        //设置默认的一页显示数
        if (pageSize==null || pageSize==0){
            pageSize=5;
        }
        //创建一个构造器
        QueryWrapper<Wage> wrapper = new QueryWrapper<>();
        if(option!=null
                && searchContent!=null
                && !"".equals(option)
                &&!"".equals(searchContent)){
            wrapper.like(option,searchContent);

        }
        //设置分页
        Page<List<Wage>> pageInfo = PageHelper.startPage(pageNum, pageSize);//返回值是Page类型的数据
        LocalDate now = LocalDate.now();
        int monthValue = now.getMonthValue();
        int dayOfMonth = now.getDayOfMonth();
        wrapper.eq("wage_month",monthValue);
        wrapper.ge("wage_day",1);
        wrapper.le("wage_day",5);
        //wrapper.eq("state","准备");
        //在登录的时候将用户记录存储在了session中
        User user =(User) SecurityUtils.getSubject().getSession().getAttribute("user");
        if("普通用户".equals(user.getDescription())){
            //如果其角色是普通用户,那么只可以查询关于自己的一系列消息,不能出现跨用户访问,否则会出现资源泄露
            wrapper.eq("user_id",user.getUserId());
        }
        iWageService.list(wrapper);
        //设置页面信息
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("option",option);
        model.addAttribute("searchContent",searchContent);
        return "view/wage";
    }
    //查询下一个月要发放的工资
    @RequestMapping("/next")
    public String next(Model  model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){
        //设置默认的页码
        if (pageNum==null || pageNum==0){
            pageNum=1;
        }
        //设置默认的一页显示数
        if (pageSize==null || pageSize==0){
            pageSize=5;
        }
        //创建一个构造器
        QueryWrapper<Wage> wrapper = new QueryWrapper<>();
        if(option!=null
                && searchContent!=null
                && !"".equals(option)
                &&!"".equals(searchContent)){
            wrapper.like(option,searchContent);

        }
        //设置分页
        Page<List<Wage>> pageInfo = PageHelper.startPage(pageNum, pageSize);//返回值是Page类型的数据
        LocalDate now = LocalDate.now();
        int monthValue = now.getMonthValue();
        int dayOfMonth = now.getDayOfMonth();
        wrapper.eq("wage_month",monthValue);
        wrapper.ge("wage_day",25);
        wrapper.le("wage_day",31);
       // wrapper.eq("state","核算中");
        //在登录的时候将用户记录存储在了session中
        User user =(User) SecurityUtils.getSubject().getSession().getAttribute("user");
        if("普通用户".equals(user.getDescription())){
            //如果其角色是普通用户,那么只可以查询关于自己的一系列消息,不能出现跨用户访问,否则会出现资源泄露
            wrapper.eq("user_id",user.getUserId());
        }
        iWageService.list(wrapper);
        //设置页面信息
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("option",option);
        model.addAttribute("searchContent",searchContent);
        return "view/wage";
    }
    @RequestMapping("/add")
    public String add(
            @RequestParam(value = "userId",required = false)Integer userId,
            @RequestParam(value = "basicPay",required = false)String basicPay,
            @RequestParam(value = "commission",required = false)String commission,
            @RequestParam(value = "deductMoney",required = false)String deductMoney,
            @RequestParam(value = "wageDate",required = false)String  wageDate){
        User user = iUserService.getById(userId);
        if(user==null){
            return "redirect:/wage/list";
        }
        //set注入
        Wage wage = new Wage();
        wage.setUserId(userId);
        wage.setBasicPay(new BigDecimal(basicPay));
        wage.setCommission(new BigDecimal(commission));
        wage.setDeductMoney(new BigDecimal(deductMoney));
        wage.setWageDate(LocalDate.parse(wageDate));
        //工资记录日期
        LocalDate wageDate1 = wage.getWageDate();
        int monthValue = wageDate1.getMonthValue();
        int dayOfMonth = wageDate1.getDayOfMonth();
        //根据工资的记录日期来设置工资发放的月份和工资发放的具体月份的天数
        wage.setWageMonth(monthValue);
        wage.setWageDay(dayOfMonth);
        //获取当前时间
        LocalDate now = LocalDate.now();
        int nowMonthValue = now.getMonthValue();
        int nowDayOfMonth = now.getDayOfMonth();
        //如果薪资进行核算的月份小于这个月,那么状态就设置为已发放
        if(monthValue<nowMonthValue){
            wage.setState("已发放");
        }
        //如果薪资进行核算的月份等于这个月,但是日期比5号小,因为工资一般都会在五号之前进行发放,
        if(monthValue==nowMonthValue  && dayOfMonth<=5){
            wage.setState("准备");
        }
        //如果信息进行核算的月份等于这个月,但是日期比25大,因为到了月底就会结合员工的具体表现进行下一个月薪资的发放
        if(monthValue==nowMonthValue && dayOfMonth>=25){
            wage.setState("核算中");
        }
        iWageService.save(wage);
        List<Wage> wageList = iWageService.list();
        List<Integer> ids = wageList.stream().map(Wage::getWageId).collect(Collectors.toList());
        Integer id = ids.get(ids.size() - 1);
        Boolean b = iWageService.getTotalSalary(id);
        return "redirect:/wage/list";
    }
    @RequestMapping("/edit")
    public String edit(
            @RequestParam(value = "wageId",required = false)Integer wageId,
            @RequestParam(value = "userId",required = false)Integer userId,
            @RequestParam(value = "basicPay",required = false)String basicPay,
            @RequestParam(value = "commission",required = false)String commission,
            @RequestParam(value = "deductMoney",required = false)String deductMoney,
            @RequestParam(value = "wageDate",required = false)String  wageDate){
        System.out.println("wageId:"+wageId);
        User user = iUserService.getById(userId);
        if(user==null){
            return "redirect:/wage/list";
        }
        //set注入
        Wage wage = new Wage();
        wage.setWageId(wageId);
        wage.setUserId(userId);
        wage.setBasicPay(new BigDecimal(basicPay));
        wage.setCommission(new BigDecimal(commission));
        wage.setDeductMoney(new BigDecimal(deductMoney));
        wage.setWageDate(LocalDate.parse(wageDate));
        LocalDate wageDate1 = wage.getWageDate();
        int monthValue = wageDate1.getMonthValue();
        int dayOfMonth = wageDate1.getDayOfMonth();
        //获取当前时间
        LocalDate now = LocalDate.now();

        int nowMonthValue = now.getMonthValue();
        //如果薪资进行核算的月份小于这个月,那么状态就设置为已发放
        if(monthValue<nowMonthValue){
            wage.setState("已发放");
        }
        //如果薪资进行核算的月份等于这个月,但是日期比5号小,因为工资一般都会在五号之前进行发放,
        if(monthValue==nowMonthValue  && dayOfMonth<=5){
            wage.setState("准备");
        }
        //如果信息进行核算的月份等于这个月,但是日期比25大,因为到了月底就会结合员工的具体表现进行下一个月薪资的发放
        if(monthValue==nowMonthValue && dayOfMonth>=25){
            wage.setState("核算中");
        }
        iWageService.updateById(wage);
        Boolean b = iWageService.getTotalSalary(wageId);
        return "redirect:/wage/list";
    }
    @RequestMapping("/del")
    public String del(@RequestParam(value = "wageId",required = false)Integer wageId){
        iWageService.removeById(wageId);
        return "redirect:/wage/list";
    }


}

